﻿using System;
using System.Linq;
using SpreadsheetGear;

namespace WorkbookDesigner.Addons.SpreadsheetEditor.Model
{
    public class PromptModel
    {
        private string _name = string.Empty;
        private string _value = string.Empty;
        private string _controlType = string.Empty;
        private string _helpMessage = string.Empty;
        private string _verifyCode = string.Empty;
        private string _comboString = string.Empty;
        private string _color = string.Empty;
        private string _picture = string.Empty;
        private string _visibleRange = string.Empty;
        private string _hideInReport = string.Empty;
        private string _tabIndex = string.Empty;
        private string _calculatorIndex = string.Empty;

        /// <summary>
        /// 根据该行创建新的变量类实例,并初始化相关属性
        /// </summary>
        /// <param name="entireRow">变量存在的整行</param>
        public PromptModel(IRange entireRow)
        {
            Row = entireRow.EntireRow;
            Name = Row.GetPName().Formula;
            Value = Row.GetPValue().Formula;
            ControlType = Row.GetPControlType().Formula;
            HelpMessage = Row.GetPHelpMessage().Formula;
            VerifyCode = Row.GetPVerifyCode().Formula;
            ComboString = Row.GetPComboString().Formula;
            Color = Row.GetPColor().Formula;
            Picture = Row.GetPPicture().Formula;
            VisibleRange = Row.GetPVisibleRange().Formula;
            HideInReport = Row.GetPHideInReport().Formula;
            TabIndex = Row.GetPTabIndex().Formula;
            CalculatorIndex = Row.GetPCalculatorIndex().Formula;
        }

        /// <summary>
        /// 创建空的变量类实例
        /// </summary>
        public PromptModel()
        {
            Row = null;
        }
        public IRange Row { get; private set; }

        public bool VisiblePrompt { get; set; }

        /// <summary>
        /// 删除该变量所在的行,同时删除对应的名称.
        /// 删除成功返回true,若Row为null则返回false
        /// </summary>
        /// <returns></returns>
        public bool Delete()
        {
            if (Row == null)
                return false;
            IWorkbook book = Row.Worksheet.Workbook;
            INames names = book.Names;
            IName name = names.OfType<IName>()
                .FirstOrDefault(n => string.Equals(n.Name, Name, StringComparison.CurrentCultureIgnoreCase));
            name?.Delete();
            Row.Delete();
            return true;
        }
        /// <summary>
        /// 将修改后的各变量值添加到初始化该实例的行范围中
        /// </summary>
        public void AddTo()
        {
            Row.GetPName().Formula = Name;
            Row.GetPValue().Formula = Value;
            Row.GetPControlType().Formula = ControlType;
            Row.GetPHelpMessage().Formula = HelpMessage;
            Row.GetPVerifyCode().Formula = VerifyCode;
            Row.GetPControlType().Formula = ComboString;
            Row.GetPColor().Formula = Color;
            Row.GetPPicture().Formula = Picture;
            Row.GetPVisibleRange().Formula = VisibleRange;
            Row.GetPHideInReport().Formula = HideInReport;
            Row.GetPTabIndex().Formula = TabIndex;
            Row.GetPCalculatorIndex().Formula = CalculatorIndex;
            INames names = Row.Worksheet.Workbook.Names;
            names.Add(Name, $"={Row.Cells["$B$1"].GetAddress(true, true, ReferenceStyle.A1, true, null)}");
            Row.Cells["$A$1:$Q$1"].HightLight();

        }
        /// <summary>
        /// 将该实例中包含的变量值按格式添加到指定的行中,同时对该行定义的名称做出修改
        /// </summary>
        /// <param name="entireRow">指定变量将添加的整行</param>
        public void AddTo(IRange entireRow)
        {
            //entireRow.DeletePrompt(false);
            string name = entireRow.GetPName().Formula;
            INames names = entireRow.Worksheet.Workbook.Names;
            var find = names.OfType<IName>().FirstOrDefault(x => x.Name == name);
            if (find == null)
                names.Add(Name, $"={entireRow.GetPValue().GetAddress(true, true, ReferenceStyle.A1, true, null)}");
            else
            {
                find.Name = Name;
                find.RefersTo = $"={entireRow.GetPValue().GetAddress(true, true, ReferenceStyle.A1, true, null)}";
            }

            entireRow.GetPName().Formula = Name;
            entireRow.GetPValue().Formula = Value;
            entireRow.GetPControlType().Formula = ControlType;
            entireRow.GetPHelpMessage().Formula = HelpMessage;
            entireRow.GetPVerifyCode().Formula = VerifyCode;
            entireRow.GetPComboString().Formula = ComboString;
            entireRow.GetPColor().Formula = Color;
            entireRow.GetPPicture().Formula = Picture;
            entireRow.GetPVisibleRange().Formula = VisibleRange;
            entireRow.GetPHideInReport().Formula = HideInReport;
            entireRow.GetPTabIndex().Formula = TabIndex;
            entireRow.GetPCalculatorIndex().Formula = CalculatorIndex;
            entireRow.Cells["$A$1:$Q$1"].HightLight();
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }

        public string ControlType
        {
            get { return _controlType; }
            set { _controlType = value; }
        }

        public string HelpMessage
        {
            get { return _helpMessage; }
            set { _helpMessage = value; }
        }

        public string VerifyCode
        {
            get { return _verifyCode; }
            set { _verifyCode = value; }
        }

        public string ComboString
        {
            get { return _comboString; }
            set { _comboString = value; }
        }

        public string Color
        {
            get { return _color; }
            set { _color = value; }
        }

        public string Picture
        {
            get { return _picture; }
            set { _picture = value; }
        }

        public string VisibleRange
        {
            get { return _visibleRange; }
            set { _visibleRange = value; }
        }

        public string HideInReport
        {
            get { return _hideInReport; }
            set { _hideInReport = value; }
        }

        public string TabIndex
        {
            get { return _tabIndex; }
            set { _tabIndex = value; }
        }

        public string CalculatorIndex
        {
            get { return _calculatorIndex; }
            set { _calculatorIndex = value; }
        }

        public override string ToString()
        {
            return _name;
        }


    }
}
